﻿using mozhi.smarterp.AuthorCenter.Enums;
using mozhi.smarterp.AuthorCenter.Identities.Clients;
using mozhi.smarterp.AuthorCenter.OpenIddict;
using mozhi.smarterp.AuthorCenter.OpenIddict.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Entities;
using Volo.Abp.OpenIddict.Applications;

namespace mozhi.smarterp.AuthorCenter.Users
{
    public class OpenIddictClientTypeAppService : AuthorCenterAppService, IOpenIddictClientTypeAppService
    {
        private IOpenIddictClientTypeRepository _repository;

        private IAbpApplicationManager _applicationManager;

        public OpenIddictClientTypeAppService(IOpenIddictClientTypeRepository repository , IAbpApplicationManager applicationManager)
        {
            _repository = repository;
            _applicationManager = applicationManager;
        }

        public async Task<OpenIddictClientTypeDto> CreateAsync(OpenIddictClientTypeCreateDto input)
        {
            var client = (await _applicationManager.FindByClientIdAsync(input.ClientId)) as OpenIddictApplicationModel;

            if (client == null)
                throw new BusinessException("100000", $"client：{input.ClientId} 不存在");

            var matchs = await _repository.GetListAsync(null , int.MaxValue , 0 , input.ClientId);

            if(matchs.Any(x=>x.ClientType == input.ClientType))
                throw new UserFriendlyException("100000", $"client：{client.ClientId}已设置了类型");

            var entity = new OpenIddictClientType(input.ClientId, input.ClientType);

            entity = await _repository.InsertAsync(entity);

            return ObjectMapper.Map<OpenIddictClientType, OpenIddictClientTypeDto>(entity);
        }

        public async Task DeleteAsync(Guid id)
        {
            await _repository.DeleteAsync(id);
        }

        public async Task<OpenIddictClientTypeDto> GetAsync(Guid id)
        {
            var entity = await _repository.GetAsync(id);

            return ObjectMapper.Map<OpenIddictClientType, OpenIddictClientTypeDto>(entity);
        }

        public async Task<PagedResultDto<OpenIddictClientTypeDto>> GetListAsync(OpenIddictClientTypeFilter filter)
        {
            var count = await _repository.GetCountAsync(filter.ClientId);

            var list = await _repository.GetListAsync(filter.Sorting, filter.MaxResultCount, filter.SkipCount, filter.ClientId);

            return new PagedResultDto<OpenIddictClientTypeDto>(
                count,
                ObjectMapper.Map<List<OpenIddictClientType>, List<OpenIddictClientTypeDto>>(list)
            );
        }

        public async Task<OpenIddictClientTypeDto> UpdateAsync(OpenIddictClientTypeUpdateDto input)
        {
            var entity = await _repository.GetAsync(input.Id);

            if (entity == null)
                throw new BusinessException("100000", $"OpenIddictUserType:{input.Id} not exist");

            entity.Change(input.ClientId, input.ClientType);

            entity = await _repository.UpdateAsync(entity);

            return ObjectMapper.Map<OpenIddictClientType, OpenIddictClientTypeDto>(entity);
        }
    }
}
